package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysOssDo;
import com.ruoyi.system.mapper.SysOssMapper;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * OSS对象存储Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysOssServiceImpl implements ISysOssService {
    private final SysOssMapper sysOssMapper;

    /**
     * 查询OSS对象存储
     */
    @Override
    public SysOssDo queryById(Long ossId) {
        return sysOssMapper.selectById(ossId);
    }

    /**
     * 查询OSS对象存储列表
     */
    @Override
    public TableDataInfo<SysOssDo> queryPageList(SysOssDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOssDo> lqw = buildQueryWrapper(entity);
        Page<SysOssDo> result = sysOssMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询OSS对象存储列表
     */
    @Override
    public List<SysOssDo> queryList(SysOssDo entity) {
        LambdaQueryWrapper<SysOssDo> lqw = buildQueryWrapper(entity);
        return sysOssMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysOssDo> buildQueryWrapper(SysOssDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysOssDo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(entity.getFileName()), SysOssDo::getFileName, entity.getFileName());
        lqw.like(StringUtils.isNotBlank(entity.getOriginalName()), SysOssDo::getOriginalName, entity.getOriginalName());
        lqw.eq(StringUtils.isNotBlank(entity.getFileSuffix()), SysOssDo::getFileSuffix, entity.getFileSuffix());
        lqw.eq(StringUtils.isNotBlank(entity.getUrl()), SysOssDo::getUrl, entity.getUrl());
        lqw.eq(StringUtils.isNotBlank(entity.getService()), SysOssDo::getService, entity.getService());
        return lqw;
    }

    /**
     * 新增OSS对象存储
     */
    @Override
    public Boolean insert(SysOssDo entity) {
        SysOssDo add = BeanUtil.toBean(entity, SysOssDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysOssMapper.insert(add) > 0;
        if (flag) {
            entity.setOssId(add.getOssId());
        }
        return flag;
    }

    /**
     * 修改OSS对象存储
     */
    @Override
    public Boolean updateById(SysOssDo entity) {
        SysOssDo update = BeanUtil.toBean(entity, SysOssDo.class);
        validEntityBeforeSave(update);
        return sysOssMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysOssDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除OSS对象存储
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysOssMapper.deleteBatchIds(ids) > 0;
    }
}
